package com.lottery.htbc.dao;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lottery.htbc.dto.BetConfigDto;
import com.lottery.htbc.dto.ConfigTaskDto;
import com.lottery.htbc.mapper.BetConfigMapper;
import com.lottery.htbc.pojo.BetConfig;


@Service
public class BetConfigDao {


	private final static Logger LOGGER = LoggerFactory.getLogger(BetConfigDao.class);
	@Autowired
	private BetConfigMapper betConfigMapper;

	public List<BetConfig> queryAll(){
		BetConfig param=new BetConfig();
		List<BetConfig> configs = betConfigMapper.select(param);
		return configs;
	}

	/**
	 * 根据任务时间(小时) 获取用户配置<br/>
	 * @param hour
	 * @return
	 */
	public List<ConfigTaskDto> queryByTaskHour(Integer hour){
		return betConfigMapper.queryByTaskHour(hour);
	}

	public List<BetConfig> queryValidAll() {
		BetConfig param=new BetConfig();
		param.setIsValid(1);
		return betConfigMapper.select(param);
	}

	public List<BetConfig> queryByInfo(BetConfig betConfig) {
		return betConfigMapper.select(betConfig);
	}

	public void updateByIsFlyback(BetConfigDto betConfig) {
		if(betConfig.getIsFlyback()){
			BetConfig param=new BetConfig();
			param.setUserId(betConfig.getUserId());
			BetConfig conf=findByInfo(param);
			if(conf!=null&&conf.getFlyback()==1){
				BetConfig updateParam=new BetConfig();
				updateParam.setId(conf.getId());
				if(conf.getZhu()==5){
					updateParam.setZhu(6);
				}
				else if(conf.getZhu()==6){
					updateParam.setZhu(5);
				}
				updateParam.setUpdated(new Date());
				betConfigMapper.updateByPrimaryKeySelective(updateParam);
				LOGGER.info("账户 {},修改注数[ {} ] to [ {} ] 成功",conf.getAccount(),conf.getZhu(),updateParam.getZhu());
			}
			else{
				LOGGER.info("账户 {},不修改注数量",conf.getAccount());
			}
		}
	}

	/**
	 * 非主键查询唯一
	 * @param param
	 * @return
	 */
	public BetConfig findByInfo(BetConfig param){
		List<BetConfig> configs=betConfigMapper.select(param);
		if(configs!=null&&configs.size()>0){
			return configs.get(0);
		}
		return null;

	}

}
